/** 构造函数继承 */
//声明父类
function SuperClass (id) {
  var name = 'javascript'  //相当于calss的 static 方法 子类无法获取
  this.books = ['javascript', 'html', 'css'];
  this.id = id
}

//声明父类原型方法
SuperClass.prototype.showBooks = function () {
  console.log(this.books)
}

//声明子类
function SubClass (id) {
  SuperClass.call(this, id)
}

//创建第一个子类实例
var subclass1 = new SubClass(10);
var subclass2 = new SubClass(11);

console.log(subclass1.books);
console.log(subclass2.id);
console.log(subclass1.name);  // undefined
// subclass2.showBooks();	// Uncaught TypeError: subclass2.showBooks is not a function	
/** 上面方法缺陷：没办法继承构造函数上的方法 showBooks() */


/** 组合式继承 */
//声明父类
var SuperFunction = function (name) {
  this.name = name;
  this.books = ['javascript', 'html', 'css']
};
//声明父类原型上的方法
SuperFunction.prototype.showBooks = function () {
  console.log(this.books)
};

//声明子类
var SubClass = function (name) {
  SuperFunction.call(this, name)

};

//子类继承父类（链式继承）   但是增加开辟了内存空间
SubClass.prototype = new SuperFunction();

//实例化子类
var superclass1 = new SubClass('java');
var superclass2 = new SubClass('php');
superclass2.showBooks();
superclass1.books.push('ios');    //["javascript", "html", "css"]
console.log(superclass1.books);  //["javascript", "html", "css", "ios"]
console.log(superclass2.books);   //["javascript", "html", "css"]




/** 寄生组合式继承 */

//原型式继承其实就是类式继承的封装,实现的功能是返回一个实例，改实例的原型继承了传入的o对象
function inheritObject (o) {
  //声明一个过渡函数对象
  function F () { }
  //过渡对象的原型继承父对象
  F.prototype = o;
  //返回一个过渡对象的实例，该实例的原型继承了父对象
  return new F();
}
//寄生式继承
//寄生式继承就是对原型继承的第二次封装，使得子类的原型等于父类的原型。并且在第二次封装的过程中对继承的对象进行了扩展
function inheritPrototype (subClass, superClass) {
  //复制一份父类的原型保存在变量中，使得p的原型等于父类的原型
  var p = inheritObject(superClass.prototype);
  //修正因为重写子类原型导致子类constructor属性被修改
  p.constructor = subClass;
  //设置子类的原型
  subClass.prototype = p;
}
//定义父类
var SuperFather = function (name) {
  this.name = name;
  this.books = ['javascript', 'html', 'css']
};
//定义父类原型方法
SuperFather.prototype.getBooks = function () {
  console.log(this.books)
};

//定义子类
var SubChild = function (name) {
  SuperFather.call(this, name)
}

inheritPrototype(SubChild, SuperFather);

var SubChildren = new SubChild('php')
console.log("SubChildren", SubChildren.name);
SubChildren.getBooks()

